package com.gz.purchase.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gz.purchase.R;
import com.gz.purchase.app.Define;


/**
 * 
 * @author wanner1991
 *
 */
public class LoadingCover extends RelativeLayout implements OnClickListener {

//	private boolean mIsRefreshing = true;
	private OnLoadingCoverRefreshListener mListener;
	
	private View mProgress;
	private TextView mHint;
	
	public LoadingCover(Context context) {
		super(context);
	}

	public LoadingCover(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public LoadingCover(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		mHint = (TextView) findViewById(R.id.tv_hint);
		mProgress = findViewById(R.id.pb_progress);
		setOnClickListener(this);
		setVisibility(View.VISIBLE);
	}

	@Override
	public void onClick(View v) {
		if(mProgress.getVisibility() == View.GONE && mListener != null) {
			refresh();
			mListener.onLoadingCoverRefresh();
		}
	}
	
	/**
	 * 刷新失败, 并且可以通过点击再次加载; 此方法为不常用方法
	 * @param failedHint 失败提示信息
	 */
	public void onRefreshFailed(String failedHint){
		mHint.setText(failedHint);
		mProgress.setVisibility(View.GONE);
//		mIsRefreshing = false;
	}
	
	/**
	 * 刷新失败, 并且可以通过点击再次加载
	 */
	public void onRefreshFailed(){
		mHint.setText(Define.MSG_LOADING_BY_CLICK);
		mProgress.setVisibility(View.GONE);
//		mIsRefreshing = false;
	}
	
	private void refresh(){
		mHint.setText(Define.MSG_UNDER_LOADING);
		mProgress.setVisibility(View.VISIBLE);
//		mIsRefreshing = true;
//		setVisibility(View.VISIBLE);
	}


	/**
	 * 刷新成功, 此view将消失
	 */
	public void onRefreshSuccess(){
		if(getVisibility() == View.VISIBLE){
			setVisibility(View.GONE);
		}
//		mIsRefreshing = false;
	}
	
	public void setOnLoadingCoverRefreshListener(OnLoadingCoverRefreshListener listener) {
		mListener = listener;
	}
	
	public static interface OnLoadingCoverRefreshListener {
		void onLoadingCoverRefresh();
	}
	
}
